package com.idreems.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.flurry.android.FlurryAgent;
import com.idreems.mm.FullWebPageActivity;
import com.idreems.mm.R;
import com.idreems.model.JsonItem;
import com.idreems.utility.TaskCallback;

public class SummaryListFragment extends ListFragment
{
	private ArrayList<JsonItem> mItems = new ArrayList<JsonItem>();

	public static SummaryListFragment newInstance(Activity context,List<JsonItem> dataItemList) {
		SummaryListFragment fragment = new SummaryListFragment();
		fragment.updateData(dataItemList);
        return fragment;
    }
	
	private void updateData(List<JsonItem> items)
	{
		if (items != null && items.size() > 0)
		{
			mItems.clear();
			mItems.addAll(items);
		}
	}
	@Override  
    public void onActivityCreated(Bundle savedInstanceState) {  
        super.onActivityCreated(savedInstanceState);  
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null)
		{
			Object obj = bundle.getSerializable(TaskCallback.RESPONSE_DATA);
			if (obj instanceof List<?>)
			{
				updateData((List<JsonItem>) obj);
			}
		}
		setListAdapter(new SummaryListAdapter(getActivity(), mItems));
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.fragment_with_list, container, false);
	}

	@Override
	public void onListItemClick(ListView parent, View v, int position, long id)
	{
		Context context = getActivity();
		String url = mItems.get(position).url;
		Intent intent = new Intent(context, FullWebPageActivity.class);
		intent.putExtra(JsonItem.WEBURL, url);
		context.startActivity(intent);
		
		HashMap<String, String> mapsHashMap = new HashMap<String, String>();
		mapsHashMap.put("onListItemClick",mItems.get(position).title);
		FlurryAgent.logEvent("onListItemClick", mapsHashMap);
	}

}
